﻿// -----------------------------------------------------------------------
// <copyright file="EventFileWriterTest.cs" company="YUMMO">
// Copyright (c) 2013 Lambert van Lieshout, YUMMO Software Development
// </copyright>
// -----------------------------------------------------------------------

namespace Yummo.BlackBox.Test
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Configuration;
    using System.IO;
    using Xunit;
    using Yummo.BlackBox;

    /// <summary>
    /// Event file writer test.
    /// </summary>
    public class EventFileWriterTest
    {
        public string _path = AppDomain.CurrentDomain.BaseDirectory;
        public string _name = "testlog-";

        [Fact]
        public void CreateFileWriter()
        {
            Assert.DoesNotThrow(delegate
            {
                // Get filename of log file.
                string logFileName = GetFileName(_path, _name, 1);
                if (File.Exists(logFileName)) File.Delete(logFileName);

                // Set up the log manager.
                BlackBoxManager log = new BlackBoxManager();

                // Creates a event writer where the output path is the same directory as library, logfile begins with testlog and the max size is 1 kilobyte.
                EventFileWriter writer = new EventFileWriter(_path, _name, 1);

                // Register the writer.
                log.RegisterWriter(BlackBoxUtilities.GetAllEventTypes(), writer.Write);

                // Write a event.
                log.Write(EventTypes.Trace, "Hello Test!");

                // Remove log file.
                if (File.Exists(logFileName)) File.Delete(logFileName);
            });
        }

        [Fact]
        public void CreateFileWriterFromConfig()
        {
            Assert.DoesNotThrow(delegate
            {
                // Get filename of log file.
                string logFileName = GetFileName(_path, _name, 1);
                if (File.Exists(logFileName)) File.Delete(logFileName);

                // Set up the log manager.
                BlackBoxManager log = new BlackBoxManager();

                // Creates and register event writer from app.config.
                EventFileWriter writer = EventFileWriter.RegisterFromConfig(log);

                // Write a event.
                log.Write(EventTypes.Trace, "Hello Test!");

                // Remove log file.
                if (File.Exists(logFileName)) File.Delete(logFileName);
                else throw new Exception("Log file is not written");
            });
        }

        [Fact]
        public void MaxFileSizeTest()
        {
            // Get filename of log file.
            string logFileNameA = GetFileName(_path, _name, 1);
            string logFileNameB = GetFileName(_path, _name, 2);
            if (File.Exists(logFileNameA)) File.Delete(logFileNameA);
            if (File.Exists(logFileNameB)) File.Delete(logFileNameB);

            // Set up the log manager.
            BlackBoxManager log = new BlackBoxManager();

            // Creates a event writer where the output path is the same directory as library, logfile begins with testlog and the max size is 1 kilobyte.
            EventFileWriter writer = new EventFileWriter(_path, _name, 1);

            // Register the writer.
            log.RegisterWriter(BlackBoxUtilities.GetAllEventTypes(), writer.Write);

            // Write a event.
            for(int i = 0; i < 5; i++)
                log.Write(EventTypes.Trace, "Hello Test!");

            bool fileAExist = File.Exists(logFileNameA);
            bool fileBExist = File.Exists(logFileNameB);

            // Check if the event is written by checking if the file exist.
            Assert.Equal<bool>(true, fileAExist);
            Assert.Equal<bool>(true, fileBExist);

            // Remove log file.
            if (File.Exists(logFileNameA)) File.Delete(logFileNameA);
            else throw new Exception("Log file A is not written");
            if (File.Exists(logFileNameB)) File.Delete(logFileNameB);
            else throw new Exception("Log file B is not written");
        }

        private static string GetFileName(string path, string name, int n)
        {
            string rawFilename = Path.Combine(path, name + DateTime.Now.ToString("yyyy-MM-dd")) + " ({0}).txt";
            return String.Format(rawFilename, n);
        }
    }
}
